"""
File shows how to read binary file with features for skin datasets.
The binary files are saved in a form:
N_features (4 bytes integer), N_labels(4 bytes integer), H (4 bytes integer), W (4 bytes integer)
Then, there is H*W rows with N_features * feature (4 bytes float), N_labels*label(4 bytes float)
"""
import struct

path = "PATH TO BINARY FILE WITH FEATURES"


def rows_generator(path):
    with open(path, "rb") as binary_file:
        n_features = struct.unpack("i", binary_file.read(4))[0]
        n_labels = struct.unpack("i", binary_file.read(4))[0]
        h = struct.unpack("i", binary_file.read(4))[0]
        w = struct.unpack("i", binary_file.read(4))[0]
        for _ in range(h * w):
            features = [struct.unpack_from("f", binary_file.read(4))[0] for _ in range(n_features)]
            labels = [struct.unpack_from("f", binary_file.read(4))[0] for _ in range(n_labels)]
            yield features, labels


for feature, label in rows_generator(path):
    print(feature)
    print(label)
